package com.renwei.yunlong.activity.me;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.SectionedSpanSizeLookup;
import com.renwei.yunlong.adapter.SpecialtyInfoListAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.CommonBean;
import com.renwei.yunlong.bean.SpecialtyInfoBean;
import com.renwei.yunlong.event.MinePageRefreshEvent;
import com.renwei.yunlong.global.EventKey;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.LogUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * 技术专长（限五个）
 * <p>
 * Created by Administrator on 2017/8/25.
 */

public class SpecialInfoActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener, SpecialtyInfoListAdapter.SpecialClickListener {


    SpecialtyInfoListAdapter adapter;
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.rw_spcial)
    RecyclerView rwSpcial;
    @Bind(R.id.tv_right)
    TextView tvRight;
    private ArrayList<String> clickList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spcial_info);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        initView();
    }

    public void initView() {
        ivBack.setVisibility(View.VISIBLE);
        tvRight.setVisibility(View.VISIBLE);
        tvTitle.setText("技术专长（限五个）");
        tvRight.setText("提交");
        ivBack.setOnClickListener(this);
        tvRight.setOnClickListener(this);
        initData();
    }

    private void initData() {
        adapter = new SpecialtyInfoListAdapter(this);
        GridLayoutManager manager = new GridLayoutManager(this, 3);
        manager.setSpanSizeLookup(new SectionedSpanSizeLookup(adapter, manager));
        rwSpcial.setAdapter(adapter);
        rwSpcial.setHasFixedSize(true);
        rwSpcial.setLayoutManager(manager);
        adapter.setOnclickListener(this);
        String ids = getIntent().getStringExtra("ids") == null ? "" : getIntent().getStringExtra("ids");
        LogUtil.e("ids" + ids);
        //当Id列表包含逗号的时候代表为数组 ，没有逗号的时候但是有数据就代表只有一条数据
        if (ids.contains(",")) {
            String[] array = ids.split(",");
            if (array.length > 0) {
                for (String item :
                        array) {
                    adapter.setItemChecked(Integer.parseInt(item), true);
                }
            }
        } else {
            if (ids.length() > 0) {
                adapter.setItemChecked(Integer.parseInt(ids), true);
            }
        }

        ServiceRequestManager.getManager().QuerySpecialty(this, "", this);
        /*//根据需要选择使用GridItemDecoration还是SpacesItemDecoration
        GridItemDecoration divider = new GridItemDecoration.Builder(this)
                .setHorizontal(R.dimen.default_divider_padding)
                .setVertical(R.dimen.default_divider_padding)
                .setColorResource(R.color.split)
                .connfig();*/
        //mRecyclerView.addItemDecoration(divider);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_right:
                String ids = "";
                HashMap map = new HashMap();
                if (clickList != null) {
                    for (int i = 0; i < clickList.size(); i++) {
                        ids = ids + clickList.get(i) + ",";
                    }
                    LogUtil.e("clickList :" + clickList.size());
                }
                if (ids.contains(",")) {
                    ids = ids.substring(0, ids.length() - 1);
                }
                LogUtil.e("ids :" + ids);
                if ("1".equals(companyType)) {
                    map.put("userId", ownerBean.getRows().getUserId());
                    map.put("technicalQualif", ids);
                    LogUtil.e("updatespecial info params" + JsonMapUtil.mapToJson(map));
                    ServiceRequestManager.getManager().UpdateSpecialty(this, JsonMapUtil.mapToJson(map), this);
                } else {
                    map.put("userId", serviceLoginBean.getRows().getUserId());
                    map.put("technicalQualif", ids);
                    LogUtil.e("updatespecial info params" + JsonMapUtil.mapToJson(map));
                    ServiceRequestManager.getManager().UpdateSpecialty(this, JsonMapUtil.mapToJson(map), this);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 885:
                LogUtil.e("查询个人专长 ：", result);
                SpecialtyInfoBean infoBean = new Gson().fromJson(result, SpecialtyInfoBean.class);
                if (infoBean != null && infoBean.getRows() != null && infoBean.getRows().size() > 0) {
                    adapter.refresh(infoBean.getRows());
                }
                break;
            case 884:
                LogUtil.e("更新个人专长结果 ：", result);
                CommonBean normalBean = new Gson().fromJson(result, CommonBean.class);
                if (normalBean.getMessage().getCode() == 200) {
                    showCenterInfoMsg("提交成功");
                    EventBus.getDefault().post(new MinePageRefreshEvent(EventKey.UPDATE_USER_SPECIAL));
                    this.finish();
                } else {
                    showCenterInfoMsg("提交失败");
                }
                break;
            default:
                break;
        }
    }

    @Override    public void onException(int requestId, String code) {        showTopWrongMsg(getResources().getString(R.string.net_work_error));    }


    @Override
    public void onSpecialClick(ArrayList<String> clickList) {
        LogUtil.e("选中列表 --- ：" + clickList.size());
        this.clickList = clickList;
    }

    @Override //最多选择五个
    public void onFiveCountClick() {
        showCenterInfoMsg("技术专长最多选择五个,您现在已经选择了5个了");
    }


    /* @Override
    public void onRefresh() {
        initData();
        rwSpcial.refreshComplete();
    }

    @Override
    public void onLoadMore() {
        rwSpcial.loadMoreComplete();
    }
*/

}
